import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class Student {
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

class  Imp implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}

public class Test2 {
    public static void main(String[] args) {
        int[] arr = {1,9,10,11,0,-1,0};
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);

        //插入数据
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }

        //此时大堆已经构建好
        for (int i = 0; i < arr.length; i++) {
            arr[arr.length - i - 1] = priorityQueue.poll();
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public int[] smallestK(int[] arr, int k) {
        //top K 问题往往都是在大量数据中找最小的 K 个值，这个 K 往往很小

        //我们建造大根堆
        //这个堆只有 k 个元素
        //之后从 k + 1 个元素遍历
        //如果比堆顶元素小，则入堆
        //因为当前堆顶元素就不是我们要的前 k 个最小的元素了


        int[] ret = new int[k];
        if (arr == null || k <= 0) {
            return ret;
        }

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Imp());
        //通过比较器构建大根堆

        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }

        for (int i = k; i < arr.length; i++) {
            if (arr[i] < priorityQueue.peek()) {
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }


        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
        //最终时间复杂度为：N*logN
    }

    public static void main4(String[] args) {

    }
    public static void main3(String[] args) {
        Imp imp = new Imp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(imp);
        //使用自己的比较器

        //堆
        priorityQueue.offer(10);
        priorityQueue.offer(5);
        System.out.println();
    }
    public static void main2(String[] args) {

        PriorityQueue<Student> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(new Student(22, "wowo"));
        priorityQueue.offer(new Student(21, "wda"));
        System.out.println();
    }

    public static void main1(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        //堆
        priorityQueue.offer(10);
        priorityQueue.offer(5);
        priorityQueue.offer(6);

        System.out.println(priorityQueue.peek());
        //当我们实例化一个 priorityQueue 之后，默认是一个小根堆
    }
}
